package com.bankcomm.audio;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

/**
 * 一个接收电话状态的广播
 * */
public class PhoneStateReceiver extends BroadcastReceiver {

	private TelephonyManager manager;
	private PhoneStateListener listener;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		// System.out.println("action:========>"+action);
		if (action.equals("android.intent.action.PHONE_STATE")) {
			manager = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
			listener = new MyPhoneStateListener();
			manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
//			manager.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE);
		}
	}

	class MyPhoneStateListener extends PhoneStateListener {

		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
				// 手机空闲
				//System.out.println("CALL_STATE_IDLE:" + state);
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				// 来电,可取到号码
				//System.out.println("CALL_STATE_RINGING:" + state + " NUMBER:" + incomingNumber);
				Message msg=new Message();
            	msg.what=1004;
            	Handler handler=EventHandler.ut.eventHandler;
            	if(null!=handler)
            	handler.sendMessage(msg);
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				// 连线时
				//System.out.println("CALL_STATE_OFFHOOK:" + state);
				break;
			}
			super.onCallStateChanged(state, incomingNumber);
		}

	}

}
